package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BizCodeEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yljf.domain.*;
import com.ruoyi.yljf.domain.dto.ActiveArticleDTO;
import com.ruoyi.yljf.domain.dto.ActiveOtherDTO;
import com.ruoyi.yljf.domain.dto.ActiveUserDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.*;
import com.ruoyi.yljf.mapper.*;
import com.ruoyi.yljf.service.IBookService;
import com.ruoyi.yljf.service.ICourseService;
import com.ruoyi.yljf.service.IUserService;
import lombok.AllArgsConstructor;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.service.IActiveOtherService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 活动绑定信息表Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-13
 */
@Service
public class ActiveOtherServiceImpl extends ServiceImpl<ActiveOtherMapper, ActiveOther> implements IActiveOtherService {

    @Resource
    private  ActiveOtherMapper activeOtherMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private ArticleTableMapper articleTableMapper;
    @Resource
    private TableMapper tableMapper;

    
    /**
     * 查询活动绑定信息表列表
     * 
     * @param activeOther 活动绑定信息表
     * @return 活动绑定信息表
     */
    @Override
    public List<ActiveOther> selectActiveOtherList(ActiveOther activeOther) {
    	LambdaQueryWrapper<ActiveOther> queryWrapper = Wrappers.lambdaQuery();
        if (ObjectUtils.isNotEmpty(activeOther.getActiveId())) {
			queryWrapper.eq(ActiveOther::getActiveId, activeOther.getActiveId());
		}
        if (ObjectUtils.isNotEmpty(activeOther.getOtherId())) {
			queryWrapper.eq(ActiveOther::getOtherId, activeOther.getOtherId());
		}
        if (ObjectUtils.isNotEmpty(activeOther.getOtherName())) {
			queryWrapper.like(ActiveOther::getOtherName, activeOther.getOtherName());
		}
        if (ObjectUtils.isNotEmpty(activeOther.getType())) {
			queryWrapper.eq(ActiveOther::getType, activeOther.getType());
		}
        return activeOtherMapper.selectList(queryWrapper);
    }

    @Override
    public List<?> getArticleSpace(List<ActiveOther> activeOthers, ActiveOtherDTO activeOtherDTO) {
//        List<ActiveOther> activeOthers = activeOtherMapper.selectList(
//                new LambdaQueryWrapper<ActiveOther>().eq(ObjectUtils.isNotEmpty(activeId),ActiveOther::getActiveId, activeId)
//                        .eq(ObjectUtils.isNotEmpty(type),ActiveOther::getType, type)
//        );
        List<?> collect = null;
        if (ObjectUtils.isNotNull(activeOthers)) {
            // 使用 Stream 提取 active_id 字段
            List<Long> ids = activeOthers.stream()
                    .map(ActiveOther::getOtherId) // 提取 active_id
                    .collect(Collectors.toList()); // 收集为列表
            if (ObjectUtils.isNotNull(ids) ) {
                if (activeOtherDTO.getType().equals(yljfEnums.getCodeByEnum(yljfEnums.ARTICLE))) {
                    List<Article> articles = articleMapper.selectBatchIds(ids);
                     collect = articles.stream().map(ActiveArticleVO::new).collect(Collectors.toList());
//                    activeOtherVO.setArticleVOList(collect);
//                    return collect;
                } else if (activeOtherDTO.getType().equals(yljfEnums.getCodeByEnum(yljfEnums.COURSE))) {
                    List<Course> courses = courseMapper.selectBatchIds(ids);
                     collect = courses.stream().map(ActiveCourseVO::new).collect(Collectors.toList());
//                    activeOtherVO.setCourseVOList(collect);
//                    return collect;
                } else if (activeOtherDTO.getType().equals(yljfEnums.getCodeByEnum(yljfEnums.USER))) {
                    List<User> users = userMapper.selectBatchIds(ids);
                     collect = users.stream().map(ActiveUserVO::new).collect(Collectors.toList());
//                    activeOtherVO.setUserVOList(collect);
//                    return collect;
                } else if (activeOtherDTO.getType().equals(yljfEnums.getCodeByEnum(yljfEnums.BOOK))) {
                    List<Book> books = bookMapper.selectBatchIds(ids);
                     collect = books.stream().map(ActiveBookVO::new).collect(Collectors.toList());
//                    activeOtherVO.setBookVOList(collect);
//                    return collect;
                } else if (activeOtherDTO.getType().equals(yljfEnums.getCodeByEnum(yljfEnums.PAPER))) {
                    //TODO  试卷表等生成通用方法之后再写
                    return new ArrayList<ActiveBookVO>();

                }
            }
        }
        return collect;
    }

    @Override
    public void addActiveArticle(ActiveArticleDTO activeArticleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(activeArticleDTO, article);
        article.setType("user");
        article.setCreateId(SecurityUtils.getUserId());
        article.setCreateType(yljfEnums.getCodeByEnum(yljfEnums.USER));
        article.setCreateId(SecurityUtils.getUserId());
        article.setCreateBy(SecurityUtils.getUsername());
        //新增文章
        articleMapper.insert(article);
            ActiveOther activeOther = ActiveOther.builder()
                    .activeId(activeArticleDTO.getAcId())
                    .type(yljfEnums.getCodeByEnum(yljfEnums.ARTICLE))
                    .otherId(article.getId()).build();
            //新增活动文章绑定信息
            activeOtherMapper.insert(activeOther);
            //文章标签绑定信息
        ArticleTable articleTable = new ArticleTable();
        articleTable.setArticleId(article.getId());
        if (ObjectUtils.isNotEmpty(activeArticleDTO.getTableId())) {
            tableMapper.addTimes(activeArticleDTO.getTableId());        //增加标签使用次数(热度)
            articleTable.setTableId(activeArticleDTO.getTableId());
        } else if (ObjectUtils.isNotEmpty(activeArticleDTO.getTableName())) {   //用户新增标签
            Table table = new Table();
            table.setTableName(activeArticleDTO.getTableName());
            table.setUseNum(1l);
            tableMapper.insert(table);
            //赋值刚新增的table的id
            articleTable.setTableId(table.getId());
        }
        //新增文章-标签绑定信息
        articleTableMapper.insert(articleTable);

    }

    @Override
    @Transactional
    public AjaxResult addActiveUser(ActiveUserDTO activeUserDTO) {
        ActiveOther activeOther1 = activeOtherMapper.selectOne(
                new LambdaQueryWrapper<>(ActiveOther.class)
                        .eq(ActiveOther::getActiveId, activeUserDTO.getActiveId())
                        .eq(ActiveOther::getType, yljfEnums.getCodeByEnum(yljfEnums.USER))
                        .eq(ActiveOther::getOtherId, SecurityUtils.getUserId()));
        if (ObjectUtils.isNotNull(activeOther1)) {
            return AjaxResult.buildResult(BizCodeEnum.USER_SIGN_UP);
        }
        ActiveOther activeOther = new ActiveOther(activeUserDTO.getActiveId(), SecurityUtils.getUserId(), yljfEnums.getCodeByEnum(yljfEnums.USER),null);
        activeOtherMapper.insert(activeOther);
        return success();
    }


}
